<?php
namespace app\admin\controller;
use think\Db;
class Game extends Base
{
    public function prize(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(isset($role)&&$role != ""){
                $map['prize_type'] = $role;
            }
            if(session ('area')){
                $map['prize_qu_id'] = ['in',session('area')];
            }else{
                $map['prize_qu_id'] = null;
            }
            if(isset($key)&&$key!="")
            {
                $map['prize_name'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('prize p')
                ->join('admin a','a.id = p.prize_a_id','left')
                ->join('area q','q.district_id = p.prize_qu_id','left')
                ->where($map)
                ->count();
            $lists =  Db::name('prize p')
                ->join('admin a','a.id = p.prize_a_id','left')
                ->join('area q','q.district_id = p.prize_qu_id','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('prize_add_time')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch ('game/prize');
    }


    public function prizeAdd(){
        if(request()->isPost ()){
            extract (input());
            $param = [
                'prize_a_id'=>session('uid'),
                'prize_num'=>$num,
                'prize_probability'=>$prob,
                'prize_qu_id'=>session('myarea'),
                'prize_type'=>$type,
                'prize_jifen'=>$jifen,
                'prize_add_time'=>date('Y-m-d H:i:s'),
                'prize_edit_time'=>date('Y-m-d H:i:s')
            ];
            if($type == 2){
                $name = Db::name('product')->where('p_id',$goods)->value('p_name');
                $param['prize_name'] = $name;
                $param['prize_goods_id'] = $goods;
            }else{
                $param['prize_name'] = "{$jifen}积分";
            }
            $res = Db::name('prize')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        $goods = Db::name('product')->where(['p_type'=>1,'p_status'=>4,'p_model'=>1])->select ();
        return $this->fetch('game/prize_add',['goods'=>$goods]);
    }


    public function prizeEdit(){
        extract (input());
        if(request()->isPost ()){
            $param = [
                'prize_num'=>$num,
                'prize_probability'=>$prob,
                'prize_type'=>$type,
                'prize_jifen'=>$jifen,
                'prize_edit_time'=>date('Y-m-d H:i:s')
            ];
            if($type == 2){
                $name = Db::name('product')->where('p_id',$goods)->value('p_name');
                $param['prize_name'] = $name;
                $param['prize_goods_id'] = $goods;
            }else{
                $param['prize_name'] = "{$jifen}积分";
            }
            $res = Db::name('prize')->where('prize_id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $data = Db::name('prize')->where('prize_id',$id)->find();
        $goods = Db::name('product')->where(['p_type'=>1,'p_status'=>4,'p_model'=>1])->select ();
        return $this->fetch ('game/prize_edit',['data'=>$data,'goods'=>$goods]);
    }


    public function prizeDel(){
        extract (input());
        $res = Db::name('prize')->where('prize_id',$id)->delete ();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }


    public function treasure(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            $map['d_status'] = ['neq',6];
            if(isset($role)&&$role != ""){
                $map['d_status'] = $role;
            }
            if(session ('area')){
                $map['shop_qu_id'] = ['in',session('area')];
            }
            if(isset($key)&&$key!="")
            {
                $map['p_name/shop_name'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('d_treasure t')
                ->join('shop s','t.d_shop_id = s.shop_id','left')
                ->join('product p','t.d_product_id = p.p_id','left')
                ->join('u_user u','t.d_win_user_id = u.user_id','left')
                ->where($map)
                ->count();
            $lists = Db::name('d_treasure t')
                ->join('shop s','t.d_shop_id = s.shop_id','left')
                ->join('product p','t.d_product_id = p.p_id','left')
                ->join('u_user u','t.d_win_user_id = u.user_id','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('d_end_time desc')
                ->select ();
            if(!empty($lists)){
                foreach ($lists as $key=>$vo){
                    $lists[$key]['p_banner'] = explode (',',$vo['p_banner']);
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $this->assign('level',session('level'));
        return $this->fetch('game/treasure');
    }


    public function doCheck(){
        extract (input());
        $res = Db::name('d_treasure')->where('d_id',$d_id)->update (['d_status'=>2]);
        $param = [
            'n_to'=>2,
            'n_title'=>'积分夺宝审核',
            'n_type'=>1,
            'n_user_id'=>$shop_id,
            'n_read'=>1,
            'n_delete'=>1,
            'n_time'=>date('Y-m-d H:i:s')
        ];
        if($type == 2){//通过
            $param['n_content'] = '积分夺宝审核通过';
        }else{//驳回
            $param['n_content'] = '积分夺宝审未通过：'.$msg;
        }
        Db::name('notice')->insert ($param);
        if($res){
            return json(['code'=>200,'msg'=>'审核成功']);
        }else{
            return json(['code'=>100,'msg'=>'审核失败']);
        }
    }


    public function treasureDel(){
        extract (input());
        $res = Db::name('d_treasure')->where('d_id',$id)->delete ();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    public function addSort(){
        extract (input());
        $res = Db::name('d_treasure')->where('d_id',$d_id)->update(['d_sort'=>$value,'d_edit_time'=>date('Y-m-d H:i:s')]);
        if($res){
            return json(['code'=>200,'msg'=>'编辑成功']);
        }else{
            return json(['code'=>100,'msg'=>'编辑失败']);
        }
    }

    public function country(){
        extract (input());
        $res = Db::name('d_treasure')->where('d_id',$id)->update(['d_country'=>$num,'d_edit_time'=>date('Y-m-d H:i:s')]);
        if($res){
            return json([]);
        }else{
            return json(['code'=>100,'msg'=>'编辑失败']);
        }
    }


    public function imgs(){
        if(request()->isAjax ()){
            extract (input ());
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('imgs')->count();
            $lists =  Db::name('imgs')
                ->page($Nowpage,$limits)
                ->order('im_add_time')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch ('game/imgs');
    }


    public function imgsAdd(){
        if(request()->isPost ()){
            extract (input());
            $param = [
                'im_image'=>$img,
                'im_add_time'=>date('Y-m-d H:i:s'),
                'im_edit_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('imgs')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch('game/imgs_add');
    }


    public function imgsEdit(){
        extract (input());
        if(request()->isPost ()){
            extract (input());
            $param = [
                'im_image'=>$img,
                'im_edit_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('imgs')->where('im_id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $data = Db::name('imgs')->where('im_id',$id)->find();
        return $this->fetch ('game/imgs_edit',['data'=>$data]);
    }

    public function imgsDel(){
        extract (input());
        $res = Db::name('imgs')->where('im_id',$id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

}